//
//  YJCycleImage.m
//  16-封装collectionView轮播图
//
//  Created by 冀永金 on 16/8/4.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "YJCycleImage.h"
#import "CyCleCollectionViewcCell.h"
@interface YJCycleImage ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UIPageControl *page;
@property (nonatomic, strong) UICollectionView *collection;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) UILabel *label;
@end
@implementation YJCycleImage
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor yellowColor];
        [self setup];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.backgroundColor = [UIColor yellowColor];
        [self setup];
    }
    return self;
}
- (void)setup {
    [self createCollectionView];
    [self createPage];
   
   
}
- (void)createCollectionView {
    _layout = [[UICollectionViewFlowLayout alloc]init];
    _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _layout.minimumLineSpacing = 0;
    _layout.minimumInteritemSpacing= 0;
    _collection = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:_layout];
    [self addSubview:_collection];
    _collection.pagingEnabled = YES;
    _collection.delegate = self;
    _collection.dataSource = self;
    [_collection registerClass:[CyCleCollectionViewcCell class] forCellWithReuseIdentifier:@"pool"];
    _collection.showsHorizontalScrollIndicator = NO;
   
}
- (void)createPage {
    self.page = [[UIPageControl alloc]init];
   // _page.backgroundColor = [UIColor yellowColor];
    [self addSubview:self.page];
   }
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _array.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CyCleCollectionViewcCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
    if (self.dataSource && [self.dataSource respondsToSelector:@selector(handleDataWithObject:cell:)]) {
        [self.dataSource handleDataWithObject:_array[indexPath.row] cell:cell];
        
    }
   cell.cycleImage.image = self.array[indexPath.row];
    cell.label.text = self.labelArray[indexPath.row];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
           
}
- (void)layoutSubviews {
    [super layoutSubviews];
    _collection.frame= self.bounds;
    _layout.itemSize = self.bounds.size;
    self.page.frame = CGRectMake(0, self.bounds.size.height - 30, self.bounds.size.width, 30);
    self.page.numberOfPages = self.array.count - 2;
    [self.page addTarget:self action:@selector(handleMove:) forControlEvents:UIControlEventValueChanged];
    [_collection setContentOffset:CGPointMake(self.bounds.size.width, 0)];
//    _label.frame = CGRectMake(0, 0, self.frame.size.width / 2, 30);
//    _label.backgroundColor = [UIColor redColor];
     [self addTimer];
}
- (void)setArray:(NSArray *)array {
    _array = array;
    [_collection reloadData];
}
- (void)setLabelArray:(NSArray *)labelArray{
    _labelArray = labelArray;
    [_collection reloadData];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x == 0) {
        [_collection scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:_array.count - 2 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    }else if (scrollView.contentOffset.x == _collection.bounds.size.width * (_array.count - 1)) {
        [_collection scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    }
    self.page.currentPage = (self.collection.contentOffset.x - self.frame.size.width ) / self.frame.size.width;
}
- (void)handleMove:(UIPageControl *)page {
    [self.collection setContentOffset:CGPointMake(self.viewForLastBaselineLayout.frame.size.width * (page.currentPage + 1), 0) animated:YES];
}
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    // 计算page算法
//    int page = (int) ((scrollView.contentOffset.x - self.frame.size.width) / self.frame.size.width + 0.5) % self.array.count;
//    self.page.currentPage = page;
//    //[self handleMove:_page];
//    
//}

- (void)addTimer {
    // 创建了timer 并默认加在loop上
    self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    // 只是创建了timer  并没有加在loop上
    //self.timer = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    
   // [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
   
    
}
- (void)nextImage {
    self.page.currentPage = (self.collection.contentOffset.x ) / self.frame.size.width;
    NSIndexPath *currrentIndexPath = [[self.collection indexPathsForVisibleItems]lastObject];
   // [self.collection scrollToItemAtIndexPath:currrentIndexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    NSInteger nextItem = currrentIndexPath.item + 1;
    if (nextItem == self.array.count - 2) {
        nextItem = 0;
    }
    [self.collection scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:nextItem inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];

  // [self handleMove:<#(UIPageControl *)#>]
    
}
- (void)removeTimer {
    [self.timer invalidate];
    self.timer = nil;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self removeTimer];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    [self addTimer];
}

    //- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    
//}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
